package com.obligatorio.domain.md;

import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;

import org.apache.log4j.Logger;

import com.obligatorio.domain.Usuario;
import com.obligatorio.domain.utils.EmailUtils;

/**
 * Message-Driven Bean implementation class for: NotificationBean
 * 
 */

@MessageDriven(activationConfig = {
		@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
		@ActivationConfigProperty(propertyName = "destination", propertyValue = "queue/NotificationQueue") })
public class NotificationBean implements MessageListener {
	private static final Logger log = Logger.getLogger(NotificationBean.class
			.getName());

	/**
	 * Default constructor.
	 */
	public NotificationBean() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see MessageListener#onMessage(Message)
	 */
	public void onMessage(Message message) {
		// TODO Auto-generated method stub
		log.info("onMessage()");
		try {
			Object msgObj = ((ObjectMessage) message).getObject();
			if (msgObj != null) {
				Usuario usr = (Usuario) msgObj;
				if (!usr.getCodeActivate().isEmpty()) {
					EmailUtils email = new EmailUtils();
					email.mailNuevoUsuario(usr);
				}else{
					// TODO Cambiar el mail.
					EmailUtils email = new EmailUtils();
					email.mailRecuperar(usr);
					log.info("Se ha enviado un mail a la casilla del usuario con los pasos para recuperar su clave.");
				}

			}
		} catch (JMSException ex) {
			log.error(ex);
		}

	}
}
